﻿using BroadGoalComponents.BGControls.Toast;
using BroadGoalComponents.Dialogs;
using Custom.Controls;
using Custom.Framework;
using Custom.Model.Dto;
using Custom.Service.Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Custom.Config
{
    public partial class frmModifyPassword : PopupFormWithCover
    {
        private SysUserRepository userDAL = new SysUserRepository();

        public frmModifyPassword()
        {
            InitializeComponent();

            DragWindow drag = new DragWindow();
            drag.BindEvent(this, this.panel1);

            this.txtYmm.InnerTextBox.PasswordChar = '*';
            this.txtXmm.InnerTextBox.PasswordChar = '*';
            this.txtXmm2.InnerTextBox.PasswordChar = '*';
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.checkData()) return;

                GlobalParam.User.password = this.txtXmm.Text;
                this.userDAL.Update(GlobalParam.User);

                BGToast.Show("密码更新成功");
                this.Close();
            }
            catch (Exception ex)
            {
                WarningDialog.Show(ex.Message);
            }
        }

        private bool checkData()
        {
            if (string.IsNullOrEmpty(this.txtYmm.Text))
            {
                WarningDialog.Show("请输入原密码");
                return false;
            }
            if (this.txtYmm.Text != GlobalParam.User?.password)
            {
                WarningDialog.Show("原密码错误");
                return false;
            }
            if (string.IsNullOrEmpty(this.txtXmm.Text))
            {
                WarningDialog.Show("请输入新密码");
                return false;
            }
            if (this.txtXmm.Text != this.txtXmm2.Text)
            {
                WarningDialog.Show("请再次输入新密码");
                return false;
            }

            return true;
        }
    }
}
